package com.huohuzhihui.web.controller.api;

import com.huohuzhihui.api.service.ApiMerchantService;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.common.utils.ServletUtils;
import com.huohuzhihui.common.utils.ip.AddressUtils;
import com.huohuzhihui.common.utils.ip.IpUtils;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.MerActivityBrowse;
import com.huohuzhihui.merchant.domain.MerCouponActivity;
import com.huohuzhihui.merchant.domain.MerMerchant;
import com.huohuzhihui.merchant.service.IMerActivityBrowseService;
import com.huohuzhihui.merchant.service.IMerCouponActivityService;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 *营销活动
 * 
 * @author lizhihe
 * @date 2020-11-22
 */
@RestController
@RequestMapping("/api/activity")
public class ApiActivityController extends BaseController
{
    @Autowired
    private IMerActivityBrowseService merActivityBrowseService;
    @Autowired
    private IMerCouponActivityService merCouponActivityService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询活动信息
     * @param id
     * @return
     */
    @GetMapping("/getActivity")
    public AjaxResult getActivity(Long id)
    {
        MerCouponActivity merMerchantActity = this.merCouponActivityService.selectMerCouponActivityById(id);
        return AjaxResult.success("查询活动详情成功",merMerchantActity);
    }


    /**
     * 查询活动信息
     * @param
     * @return
     */
    @GetMapping("/findActivityList")
    public AjaxResult findActivityList(HttpServletRequest request)
    {
        LoginUser loginUser =tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();
        MerCouponActivity merMerchantActity = new MerCouponActivity();
        merMerchantActity.setMerchantId(sysUser.getDeptId());
        return AjaxResult.success("查询商户活动信息成功",this.merCouponActivityService.selectMerCouponActivityList(merMerchantActity));
    }


    /**
     * 插入一条活动浏览记录
     * @param merchantId
     * @param activityId
     * @return
     */
    @GetMapping("/saveBrowseRecord")
    @ResponseBody
    public AjaxResult saveBrowseRecord(Long merchantId,Long activityId)
    {
        //插入浏览记录
        MerActivityBrowse merActivityBrowse = new MerActivityBrowse();
        merActivityBrowse.setMerchantId(merchantId);
        merActivityBrowse.setActivityId(activityId);
        merActivityBrowse.setCreateTime(DateUtils.getNowDate());

        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        merActivityBrowse.setIp(ip);
        merActivityBrowse.setLocation(AddressUtils.getRealAddressByIP(ip));

        UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        merActivityBrowse.setBrowser(userAgent.getBrowser().getName());
        merActivityBrowse.setOs(userAgent.getOperatingSystem().getName());
        merActivityBrowseService.insertMerActivityBrowse(merActivityBrowse);

        return AjaxResult.success();
    }
}
